#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QElapsedTimer>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    motorCOM=new gyro_bot_motor("/dev/ttyS3");
    motorCOM->Connect();
    spd=200;
    ui->lcdNumber->setPalette(Qt::red);
    this->fetch_timer = new QTimer;
    fetch_timer->start(400);
    connect(fetch_timer,SIGNAL(timeout()),this,SLOT(readMotorCOM()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readMotorCOM()
{
    QByteArray temp = motorCOM->MotorCom->readAll();
    ui->textEdit->insertPlainText(temp);
}

void MainWindow::on_pushButton_clicked()
{
    motorCOM->SetSPD_SVS("1",750);
    motorCOM->SetSPD_SVS("2",750);
    motorCOM->SetSPD_SVS("1",750);
    motorCOM->SetSPD_SVS("2",750);
    motorCOM->SetSPD_SVS("1",750);
    motorCOM->SetSPD_SVS("2",750);
    QElapsedTimer t;
    t.start();
    while(t.elapsed()<7000)
        QCoreApplication::processEvents();
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
}

void MainWindow::on_pushButton_2_clicked()
{
    motorCOM->SetSPD_SVS("1",0);
    motorCOM->SetSPD_SVS("2",0);
    motorCOM->SetSPD_SVS("1",0);
    motorCOM->SetSPD_SVS("2",0);
    motorCOM->SetSPD_SVS("1",0);
    motorCOM->SetSPD_SVS("2",0);
}

void MainWindow::on_pushButton_3_clicked()
{
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",this->spd);
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",this->spd);
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",this->spd);
}

void MainWindow::on_pushButton_4_clicked()
{
    motorCOM->SetSPD_SVS("1",this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
    motorCOM->SetSPD_SVS("1",this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
    motorCOM->SetSPD_SVS("1",this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
}

void MainWindow::on_pushButton_5_clicked()
{
    motorCOM->SetSPD_SVS("1",this->spd);
    motorCOM->SetSPD_SVS("2",this->spd);
    motorCOM->SetSPD_SVS("1",this->spd);
    motorCOM->SetSPD_SVS("2",this->spd);
    motorCOM->SetSPD_SVS("1",this->spd);
    motorCOM->SetSPD_SVS("2",this->spd);
}

void MainWindow::on_pushButton_6_clicked()
{
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
    motorCOM->SetSPD_SVS("1",-1*this->spd);
    motorCOM->SetSPD_SVS("2",-1*this->spd);
}

void MainWindow::on_verticalSlider_valueChanged(int value)
{
    this->spd=ui->verticalSlider->value();
    ui->lcdNumber->display(this->spd);
}

void MainWindow::on_pushButton_7_clicked()
{
    motorCOM->GetVol();
}

void MainWindow::on_pushButton_8_clicked()
{
    ui->textEdit->clear();
}
